//  -----------------------------------------------------
//  Project RenderIt
//  Contact : hzzhoukeli@corp.netease.com
//  Author : zhoukeli
//  -----------------------------------------------------
/// @file
/// @path
/// @data 2016/05/04
/// @brief

#pragma once

#include "public/build.h"

namespace renderit
{
	namespace math
	{
		class vector2
		{
		public:
			vector2(float x_, float y_) : x(x_), y(y_) {}
			vector2() : x(0), y(0) {}
			vector2(float v) : x(v), y(v) {}
			vector2(const vector2 & rh)
			{
				this->operator=(rh);
			}

		public:
			vector2 & operator = (const vector2 & rh)
			{
				if (&rh != this) {
					x = rh.x;
					y = rh.y;
				}
				return *this;
			}

			vector2 & operator += (const vector2 & rh)
			{
				if (&rh != this) {
					x += rh.x;
					y += rh.y;
				}
				return *this;
			}

			vector2 & operator -= (const vector2 & rh)
			{
				if (&rh != this) {
					x -= rh.x;
					y -= rh.y;
				}
				return *this;
			}

			vector2 & operator *= (float v)
			{
				x *= v;
				y *= v;
				return *this;
			}

			vector2 & operator /= (float v)
			{
				if (v) {
					x /= v;
					y /= v;
				}
				return *this;
			}

			vector2 operator * (float f) const
			{
				vector2 v(*this);
				v *= f;
				return v;
			}

			vector2 operator / (float f) const
			{
				vector2 v(*this);
				v /= f;
				return v;
			}

			vector2 operator - (const vector2 & rh) const
			{
				vector2 v(*this);
				v -= rh;
				return v;
			}

			friend vector2 operator * (float f, const vector2 & v);

		public:
			float dot(const vector2 & rh) const
			{
				return x * rh.x + y * rh.y;
			}

			float length2() const
			{
				return x * x + y * y;
			}

			float length() const
			{
				return sqrt(this->length2());
			}

			void normalize()
			{
				float len = length();
				if (len > 0) {
					x /= len;
					y /= len;
				}
			}
		public:
			float x, y;
		};

		inline vector2 operator * (float f, const vector2 & v)
		{
			return v * f;
		}
	}	
}